﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Wasion.HHU.Data.Models;
using Wasion.HHU.Data.Repositories;
using SQLite.Net;
using System;
using System.Linq.Expressions;

namespace Wasion.HHU.Data.Repositories
{
    public class SchemaItemParDbRepository : BaseDbRepository
    {
        public SchemaItemParDbRepository() : base()
        {

        }
        public SchemaItemParDbRepository(SQLiteConnection connection)
            : base(connection)
        {
        }

        public Task<List<SchemaItemPar>> GetAllList()
        {
            List<SchemaItemPar> schemaItemPars;

            lock (databaseLock)
            {
                schemaItemPars = DbConnection.Table<SchemaItemPar>().
                    OrderBy(x => x.SchID).
                    ToList();
            }
            return Task.FromResult(schemaItemPars);
        }

        public Task<List<SchemaItemPar>> GetList(Expression<Func<SchemaItemPar, bool>> filter)
        {
            List<SchemaItemPar> schemaItemPars;

            lock (databaseLock)
            {
                schemaItemPars = DbConnection.Table<SchemaItemPar>().
                    OrderByDescending(x => x.SchID).
                    Where(filter).
                    ToList();
            }
            return Task.FromResult(schemaItemPars);
        }

        public void Insert(SchemaItemPar schemaItemPar)
        {
            lock (databaseLock)
            {
                DbConnection.CreateTable<SchemaItemPar>();
                DbConnection.Insert(schemaItemPar);
            }
        }

        public void Update(SchemaItemPar schemaItemPar)
        {
            lock (databaseLock)
            {
                DbConnection.Update(schemaItemPar);
            }
        }

        public SchemaItemPar Find(object primaryKey)
        {
            SchemaItemPar schemaItemPar;

            lock (databaseLock)
            {
                schemaItemPar = DbConnection.Find<SchemaItemPar>(primaryKey);
            }
            return schemaItemPar;
        }

        public void Delete(object primaryKey)
        {
            lock (databaseLock)
            {
                DbConnection.Delete<SchemaItemPar>(primaryKey);
            }
        }
    }
}
